<?php
// +----------------------------------------------------------------------
// | Fanwe 方维众筹商业系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 甘味人生(526130@qq.com)
// +----------------------------------------------------------------------

class BmUserAccountAction extends CommonAction
{
    public function index()
    {		
		$login_name=strim($_REQUEST['login_name']);
        $user_id=intval($_REQUEST['user_id']);
        $bm_qrcode_id=intval($_REQUEST['bm_qrcode_id']);
		$login_name=strim($_REQUEST['login_name']);
		$login_name1=strim($_REQUEST['login_name1']);

        /*$time = $this->check_date();
		if($time['status']==0)
		{
			$this->error($time['error']);
		}*/
		
        //$where="u.is_robot =0 and bm_pid>0  and p.member_id =0";
		$where="u.is_robot =0 and u.bm_pid>0 and p.member_id =0  and p.type=2";

        if($user_id >0){
            $where .=" and u.id =".$user_id."";
        }
		if($login_name){
     		$where .=" and p.login_name = '".$login_name."' ";
     	}elseif($login_name1){
     		$where .= " and p.login_name like '%" . $login_name1 . "%' ";
     	}
     	
     	if($bm_qrcode_id != ""){
     		$where .= " and  u.bm_qrcode_id = ".$bm_qrcode_id." ";
     	}
		
		//排序字段 默认为主键名
		if (isset ( $_REQUEST ['_order'] )) {
			$order = strim($_REQUEST ['_order']);
		} else {
			$order = "u.id";
		}
		
		if (isset ( $_REQUEST ['_sort'] )) {
			$sort = $_REQUEST ['_sort'] ? 'asc' : 'desc';
		} else {
			$sort = $asc ? 'asc' : 'desc';
		}
		
		$pre = DB_PREFIX;
		
        $sql_count="select 
						count(*) as list_count,
						sum(u.diamonds) as total_diamonds,
						sum(u.coin) as total_coins 
					from 
						".$pre."user as u 
					left join ".$pre."bm_promoter as p on p.user_id =u.bm_pid 
					where 
						".$where;
        

		$list_count_row=$GLOBALS['db']->getRow($sql_count);
		
		$p = $_REQUEST['p'];
		if ($p == '') {
			$p = 1;
		}
		$p         = $p > 0 ? $p : 1;
		$page_size = 10;
		$limit     = (($p - 1) * $page_size) . "," . $page_size;

		$count     = intval($list_count_row['list_count']);

        if($count > 0){

			$page      = new Page($count, $page_size);
			$page_show = $page->show();
	
			 $sql="select 
						u.id,
						u.nick_name,
						u.diamonds,
						u.coin,
						u.bm_pid,
						u.is_effect,
						u.bm_qrcode_id,
						p.name as p_name,
						p.login_name as p_login_name,
						q.name as qrcode_name,
						q.promoter_id as qrcode_promoter_id 
					from ".$pre."user as u
					left join ".$pre."bm_promoter as p on p.user_id =u.bm_pid 
					left join ".$pre."bm_qrcode as q on q.id = u.bm_qrcode_id 
					where 
						".$where." order by ".$order." ".$sort." limit ".$limit;
			
			$list = $GLOBALS['db']->getAll($sql);
			
			foreach($list as $k=>$v){
				$qrcode_promoter_ids[]=$v['qrcode_promoter_id'];
			}
			$qrcode_promoter_ids=array_filter($qrcode_promoter_ids);
			if($qrcode_promoter_ids){
				$qrcode_promoter_names=$GLOBALS['db']->getAll("select id,name,login_name from ".$pre."bm_promoter where id in(".implode(',',$qrcode_promoter_ids).") ");
				foreach($qrcode_promoter_names as $k=>$v){
					$promoter_names[$v['id']]['name']=$v['name'];
					$promoter_names[$v['id']]['login_name']=$v['login_name'];
				}
	
				foreach($list as $k=>$v){
					$list[$k]['qrcode_promoter_name']=$promoter_names[$v['qrcode_promoter_id']]['name'];
					$list[$k]['qrcode_promoter_login_name']=$promoter_names[$v['qrcode_promoter_id']]['login_name'];
				}
			}
		}
		
		$sortImg = $sort; //排序图标
		$sortAlt = $sort == 'desc' ? l("ASC_SORT") : l("DESC_SORT"); //排序提示
		$sort = $sort == 'desc' ? 1 : 0; //排序方式
		$this->assign ( 'list', $redis['list'] );
		$this->assign ( 'sort', $sort );
		$this->assign ( 'order', $order );
		$this->assign ( 'sortImg', $sortImg );
		$this->assign ( 'sortType', $sortAlt );
		
        $this->assign('list', $list);
        $this->assign('page', $page_show);
		$this->assign('total_diamonds', $list_count_row['total_diamonds']);
		$this->assign('total_coins', $list_count_row['total_coins']);
		$this->assign('user_id', $user_id);
		$this->assign('bm_qrcode_id', $bm_qrcode_id);
		$this->assign('login_name', $login_name);
		$this->assign('login_name1', $login_name1);
		
		$bm_config = load_auto_cache("bm_config");
		$this->assign('bm_two', $bm_config['bm_two']);
		
        $this->display();
    }
	
	//导出电子表
    public function export_csv($page = 1)
    {

        $pagesize = 10;
        set_time_limit(0);
        $limit = (($page - 1)*intval($pagesize)).",".(intval($pagesize));
		
		$date = $_REQUEST;
		
		/*********************/
		
		/*------------------------*/
		$login_name=strim($_REQUEST['login_name']);
        $user_id=intval($_REQUEST['user_id']);
        $bm_qrcode_id=intval($_REQUEST['bm_qrcode_id']);
		$login_name=strim($_REQUEST['login_name']);
		$login_name1=strim($_REQUEST['login_name1']);

        /*$time = $this->check_date();
		if($time['status']==0)
		{
			$this->error($time['error']);
		}*/
		
        //$where="u.is_robot =0 and bm_pid>0  and p.member_id =0";
		$where="u.is_robot =0 and u.bm_pid>0 and p.member_id =0  and p.type=2";

        if($user_id >0){
            $where .=" and u.id =".$user_id."";
        }
		if($login_name){
     		$where .=" and p.login_name = '".$login_name."' ";
     	}elseif($login_name1){
     		$where .= " and p.login_name like '%" . $login_name1 . "%' ";
     	}
     	
     	if($bm_qrcode_id != ""){
     		$where .= " and  u.bm_qrcode_id = ".$bm_qrcode_id." ";
     	}
		
		//排序字段 默认为主键名
		if (isset ( $_REQUEST ['_order'] )) {
			$order = strim($_REQUEST ['_order']);
		} else {
			$order = "u.id";
		}
		
		if (isset ( $_REQUEST ['_sort'] )) {
			$sort = $_REQUEST ['_sort'] ? 'asc' : 'desc';
		} else {
			$sort = $asc ? 'asc' : 'desc';
		}
		
		$pre = DB_PREFIX;
		
        $sql_count="select 
						count(*) as list_count,
						sum(u.diamonds) as total_diamonds,
						sum(u.coin) as total_coins 
					from 
						".$pre."user as u 
					left join ".$pre."bm_promoter as p on p.user_id =u.bm_pid 
					where 
						".$where;
        

		$list_count_row=$GLOBALS['db']->getRow($sql_count);

		$count     = intval($list_count_row['list_count']);

        if($count > 0){
	
			 $sql="select 
						u.id,
						u.nick_name,
						u.diamonds,
						u.coin,
						u.bm_pid,
						u.is_effect,
						u.bm_qrcode_id,
						p.name as p_name,
						p.login_name as p_login_name,
						q.name as qrcode_name,
						q.promoter_id as qrcode_promoter_id 
					from ".$pre."user as u
					left join ".$pre."bm_promoter as p on p.user_id =u.bm_pid 
					left join ".$pre."bm_qrcode as q on q.id = u.bm_qrcode_id 
					where 
						".$where." order by ".$order." ".$sort." limit ".$limit;
			
			$list = $GLOBALS['db']->getAll($sql);
			
			foreach($list as $k=>$v){
				$qrcode_promoter_ids[]=$v['qrcode_promoter_id'];
			}
			$qrcode_promoter_ids=array_filter($qrcode_promoter_ids);
			if($qrcode_promoter_ids){
				$qrcode_promoter_names=$GLOBALS['db']->getAll("select id,name,login_name from ".$pre."bm_promoter where id in(".implode(',',$qrcode_promoter_ids).") ");
				foreach($qrcode_promoter_names as $k=>$v){
					$promoter_names[$v['id']]['name']=$v['name'];
					$promoter_names[$v['id']]['login_name']=$v['login_name'];
				}
	
				foreach($list as $k=>$v){
					$list[$k]['qrcode_promoter_name']=$promoter_names[$v['qrcode_promoter_id']]['name'];
					$list[$k]['qrcode_promoter_login_name']=$promoter_names[$v['qrcode_promoter_id']]['login_name'];
				}
			}
		}
		else
		{
			if($page==1)
				$this->error(L("NO_RESULT"));
		}

		/*------------------------*/
		/*********************/
		$voList = $list;
		
        if($voList)
        {
			$sortImg = $sort; //排序图标
			$sortAlt = $sort == 'desc' ? l("ASC_SORT") : l("DESC_SORT"); //排序提示
			$sort = $sort == 'desc' ? 1 : 0; //排序方式
			//模板赋值显示
	
			
			$bm_config = load_auto_cache("bm_config");
			
            register_shutdown_function(array(&$this, 'export_csv'), $page+1);

            $refund_value = array( 'id'=>'""', 'nick_name'=>'""', 'coin'=>'""','diamonds'=>'""','p_login_name'=>'""','qrcode_promoter_login_name'=>'""','bm_qrcode_id'=>'""');
            if($page == 1)
            {
                $content = iconv("utf-8","gbk","会员ID,会员名称,剩余游戏币,剩余钻石,所属".$bm_config['bm_two']."编号,所属代理商编号,所属业务员ID");
                $content = $content . "\n";
            }


            foreach($voList as $k=>$v)
            {
				$refund_value['id'] = '"' . iconv('utf-8','gbk',$v['id']) . '"'."\t";
				$refund_value['nick_name'] = '"' . iconv('utf-8','gbk',$v['nick_name']) . '"'."\t";
				$refund_value['coin'] = '"' . iconv('utf-8','gbk',$v['coin']) . '"'."\t";
				$refund_value['diamonds'] = '"' . iconv('utf-8','gbk',$v['diamonds']) . '"'."\t";
				$refund_value['p_login_name'] = '"' . iconv('utf-8','gbk',$v['p_login_name']) . '"'."\t";
				$refund_value['qrcode_promoter_login_name'] = '"' . iconv('utf-8','gbk',$v['qrcode_promoter_login_name']) . '"'."\t";
				$refund_value['bm_qrcode_id'] = '"' . iconv('utf-8','gbk',$v['bm_qrcode_id']) . '"'."\t";
				
                $content .= implode(",", $refund_value) . "\n";
            }
            //
            header("Content-Disposition: attachment; filename=refund_list.csv");

            echo $content ;
        }
        else
        {
            if($page==1)
                $this->error(L("NO_RESULT"));
        }

    }
}
